<template>
  <div class="app-container">
    <el-row :gutter="24" type="flex" justify="between" class="padding">
      <el-col :span="12" class="titles">
        <span class="title">菜单列表</span>
      </el-col>
      <el-col :span="12" class="right">
        <el-button type="primary" @click="handleAdd" size="medium">新增菜单</el-button>
      </el-col>
    </el-row>
    <el-table
      v-loading="listLoading"
      :data="list"
      row-key="id"
      border
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column
        type="index"
        width="50">
      </el-table-column>
      <el-table-column
        prop="name"
        label="菜单名称"
        width="180"
      />
      <el-table-column
        prop="icon"
        label="图标"
        width="180"
      >
        <template slot-scope="scope">
          <i :class="scope.row.icon" />
        </template>
      </el-table-column>
      <el-table-column
        prop="sort"
        label="排序"
        width="80"
      />
      <el-table-column
        prop="level"
        label="层级"
        width="80"
      />
      <el-table-column
        prop="pathUrl"
        label="地址"
      />
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="title" width="40%" :visible.sync="dialogForm" @close="colse">
      <el-form ref="form" :model="form" size="mini" :rules="rules">
        <el-form-item label="活动名称" :label-width="formLabelWidth" class="flex">
          <el-radio-group v-model="form.type" style="margin-bottom: 20px;" >
            <el-radio-button :label="1">目录</el-radio-button>
            <el-radio-button :label="2">菜单</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="菜单名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.name" placeholder="请输入菜单名称"></el-input>
        </el-form-item>
        <el-form-item label="上级菜单" :label-width="formLabelWidth" v-if="form.type == 2" prop="parentId">
          <el-cascader
            v-model="form.parentId"
            :options="list"
            :props="{ checkStrictly: true, value: 'id', label: 'name',children: 'children', multiple: false}"
            placeholder="请选择上级菜单"></el-cascader>
        </el-form-item>
        <el-form-item label="排序" :label-width="formLabelWidth" prop="sort">
          <el-input v-model="form.sort"  placeholder="请输入排序" oninput ="value=value.replace(/[^0-9.]/g,'')"></el-input>
        </el-form-item>
        <el-form-item label="菜单别名" :label-width="formLabelWidth" prop="aliasName">
          <el-input v-model="form.aliasName" placeholder="请输入菜单别名"></el-input>
        </el-form-item>
        <el-form-item label="路由地址" :label-width="formLabelWidth" prop="pathUrl">
          <el-input v-model="form.pathUrl" placeholder="请输入路由地址"></el-input>
        </el-form-item>
        <el-form-item label="目录图标" :label-width="formLabelWidth" v-if="form.type == 1" prop="icon">
          <el-input v-model="form.icon" placeholder="请输入目录图标"></el-input>
        </el-form-item>
        <el-form-item label="隐藏菜单" :label-width="formLabelWidth" v-if="form.type == 2">
          <el-radio-group v-model="form.hiddenFlag" style="margin-bottom: 20px;" >
            <el-radio-button :label="0">是</el-radio-button>
            <el-radio-button :label="1">否</el-radio-button>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogForm = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getAllMenu, menuRemove, saveMenu } from '@/api/admin/mune'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      dialogForm: false,
      form: {
        type: 1,
        name: '',
        sort: '',
        aliasName: '',
        pathUrl: '',
        icon: '',
        parentId: '',
        hiddenFlag: 1
      },
      rules: {
        name: [
          { required: true, message: '请输入菜单名称\n', trigger: 'blur' },
        ],
        sort: [
          { required: true, message: '请输入排序', trigger: 'change' }
        ],
        aliasName: [
          { required: true, message: '请输入别名', trigger: 'change' }
        ],
        pathUrl: [
          { required: true, message: '请输入路由地址', trigger: 'change' }
        ],
        icon: [
          { required: true, message: '请输入图标', trigger: 'change' }
        ],
        parentId: [
          {  required: true, message: '请选择上级菜单', trigger: 'change' }
        ]
      },
      title: '',
      formLabelWidth: '120px',
      optionProps: {
        value: 'id',
        label: 'name',
        children: 'children',
        checkStrictly: true
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    colse() {
      this.form = {
        type: 1,
        name: '',
        sort: '',
        aliasName: '',
        pathUrl: '',
        icon: '',
        parentId: '',
        hiddenFlag: 1
      }
    },
    handleAdd() {
      this.dialogForm = true
      this.title = '新增菜单'
    },
    handleEdit(index, row) {
      this.dialogForm = true
      this.form = JSON.parse(JSON.stringify(row))
      this.form.parentId = [row.parentId]
      this.title = '修改菜单'
    },
    submit() {
      this.$refs.form.validate((vaild,done) => {
        if (vaild) {
          if (this.form.parentId) {
            this.form.parentId = this.form.parentId[0]
          }
          saveMenu(this.form).then(response => {
            this.getList()
            this.dialogForm = false
            this.form = {
              type: 1,
              name: '',
              sort: '',
              aliasName: '',
              pathUrl: '',
              icon: '',
              parentId: '',
              hiddenFlag: 1
            }
            this.$message({
              showClose: true,
              message: response.message,
              type: 'success'
            })
          })
        }
      })
    },
    handleDelete(index, row) {
      this.$confirm(
        '删除',
        '删除', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(() => {
          menuRemove(row.id).then(response => {
            this.getList()
            this.$message({
              showClose: true,
              message: response.message,
              type: 'success'
            })
          })
        })
    },
    getList() {
      this.listLoading = true
      getAllMenu().then(response => {
        this.list = response.data
        this.listLoading = false
      })
    }
  }
}
</script>

<style>
/deep/.el-form-item{
  margin-bottom: 0 !important;
}
</style>

<style scoped>
.title{
  padding-left: 7px;
  font-size: 16px;
  font-weight: 500;
  line-height: 24px;
}
.right{
  display: flex;
  justify-content: flex-end;
}
.padding{
  margin-bottom: 20px;
}
.titles{
  display: flex;
  align-items: center;
}
.flex{
  padding-top: 20px;
}
</style>
